<?php

namespace App\Exports;

use App\Models\Clue;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class ClueExport implements FromQuery, WithHeadings, ShouldAutoSize, WithMapping
{
    use Exportable;

    public static $typeList = [
        'zx' => '整形',
        'kq' => '口腔',
    ];

    public static $clueTypeList = [
        '表单提交',
        '在线咨询',
        '智能电话',
        '网页回呼',
        '卡券',
        '抽奖',
    ];

    public static $clueSource = [
        '外部流量',
        '正常投放',
        '外部导入',
        '异常提交',
        '广告预览',
        '抖音私信',
        '鲁班线索'
    ];

    public static $gender = [
        '未知',
        '男',
        '女'
    ];

    public function __construct(string $type, string $start, string $end)
    {
        $this->type  = $type;
        $this->start = $start;
        $this->end   = $end;
    }

    public function query()
    {
        $query = Clue::query()->with(['distributions', 'tags', 'account']);
        $query->whereDate('create_time', '>=', $this->start);
        $query->whereDate('create_time', '<=', $this->end);

        $request = request();
        $field   = $request->get('order_field', 'create_time');
        $dis     = $request->get('order_dis', 'desc');
        $query->orderBy($field, $dis);

        if ($this->type !== 'my') {
            $query->where('type', $this->type);
        } else {
            $user = Auth::guard('api')->user();
            $query->whereHas('distributions', function ($q) use ($user) {
                $q->where('user_id', $user->id);
            });
        }
        return $query;
    }

    /**
     * @var Clue $clue
     * @return array
     */
    public function map($clue): array
    {
        $distributions = $clue->distributions->toArray();

        $distributions = $distributions ? $distributions[0]['name'] : '';
        return [
            $clue->id,//'#',
            $clue->account->name,//'所属账户名称',
            self::$typeList[$clue->type],//'账户类型',
            $clue->appname,//'流量来源',
            $clue->name,//'姓名',
            $clue->telphone,//'电话',
            $distributions ? '已分配' : '未分配',//'线索状态',
            $distributions,//'线索分配',
            self::$gender[$clue->gender],//'性别',
            $clue->age ?: 0,//'年龄',
            $clue->convert_status,//'转化状态',
            $clue->ad_plan_name,//'广告计划名称',
            self::$clueTypeList[$clue->clue_type],//'组件类型',
            self::$clueSource[$clue->clue_source],//'线索来源',
            json_encode($clue->remark_dict, JSON_UNESCAPED_UNICODE),//'自定义选项',
            $clue->external_url,//'落地页 url',
            $clue->module_name,//'组件名称',
            $clue->create_time,//'线索提交时间',
            $clue->clue_id,//'线索 id',
            $clue->adv_id,//'广告主 id',
            $clue->ad_plan_id,//'广告计划 id',
            $clue->site_id,//'站点 id',
            $clue->module_id,//'组件 id',
            $clue->email,//'邮箱',
            $clue->weixin,//'微信',
            $clue->qq,//'qq',
            $clue->province_name,//'省份',
            $clue->city_name,//'城市',
            $clue->address,//'详细地址',
            $clue->remark,//'用户备注留言',
            $clue->form_remark,//'表单自定义字段',
        ];
    }

    public function headings(): array
    {
        return [
            '#',
            '所属账户名称',
            '账户类型',
            '流量来源',
            '姓名',
            '电话',
            '线索状态',
            '线索分配',
            '性别',
            '年龄',
            '广告计划名称',
            '组件类型',
            '线索来源',
            '自定义选项',
            '落地页 url',
            '组件名称',
            '线索提交时间',
            '线索 id',
            '广告主 id',
            '广告计划 id',
            '站点 id',
            '组件 id',
            '邮箱',
            '微信',
            'qq',
            '省份',
            '城市',
            '详细地址',
            '用户备注留言',
            '表单自定义字段',
            '转化状态',
        ];
    }
}
